package com.wude.guild;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.LinearLayout;

import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.guild.dialog.DoubleButtonDialog;
import com.wude.guild.face.DoubleButtonFace;
import com.wude.guild.face.LuckViewFace;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.guild.views.LuckPanView;
import com.wude.http.HttpCom;
import com.wude.tools.Util;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 抽奖
 */
public class ChoujiangActivity extends TitleBaseActivity implements LuckViewFace, DoubleButtonFace {
    @BindView(R.id.ll_luckPan)
    LinearLayout ll_luckPan;
    private LuckPanView luckPanView;
    private String phone;
    private int dayLootery;
    private DoubleButtonDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choujiang);
        ButterKnife.bind(this);

        setHeaderTitle("抽奖");

        phone = getIntent().getStringExtra("phone");

        if (TextUtils.isEmpty(phone)) {
            luckPanView = new LuckPanView(this, this, this);
            dayLootery = getIntent().getIntExtra("dayLootery", 0);
        } else {
            luckPanView = new LuckPanView(this, this, phone);
        }
        ll_luckPan.addView(luckPanView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    Util util = Util.getInstance();

    @Override
    public void isFirst() {
        if (dayLootery == 0 && util.getIsFree(this) == 0) {// 不是第一次抽奖  展示弹窗
            dialog = new DoubleButtonDialog(this, this);
            dialog.show();
        } else if (dayLootery == 1) {//当天第一次抽奖  改变弹窗展示状态为
            util.setIsFree(this, 0);
            luckPanView.rotation();
        } else {
            luckPanView.rotation();
        }

        //刷新dayLootery的值
        refreshRedIcon();
    }

    public void refreshRedIcon() {
        Map<String, String> map = new HashMap<>();
        map.put("token", Utils.getLoginUser() == null ? "" : Utils.getLoginUser().token);
        HttpCom.POST(redHandler, HttpCom.getGetRedIconStatus(), map, false);
    }

    /**
     * 刷新dayLootery的值
     */
    @SuppressLint("HandlerLeak")
    Handler redHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        JSONObject data = jsonObject.getJSONObject("data");
                        dayLootery = data.getInt("dayLottery");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络错误");
                    break;
            }
        }
    };

    @Override
    public void kown() {
        dialog.dismiss();
        luckPanView.canGoOn(true);
        //改变弹窗状态为  要显示
        util.setIsFree(this, 0);
    }

    @Override
    public void goon() {
        dialog.dismiss();
        luckPanView.canGoOn(true);
        util.setIsFree(this, 1);
    }
}
